﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enter.Common;
using Enter.BizLogic;
using Enter.Entity;
using Enter.Config;

namespace Enter.Web.SystemAdmin.GoodClass
{
    public partial class importClassProperty : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] files = FileHelper.File_List("G:\\项目\\网上商城\\属性\\");
            int code = 1;
            foreach (string file in files)
            {
                string content = FileHelper.readFile(file, "GB2312");
                string filename = file.Substring(file.LastIndexOf("\\") + 1, file.LastIndexOf(".") - file.LastIndexOf("\\") - 1);

                string[] str1 = content.Replace("\r\n", "").Replace("\t", "").Trim('；').Split('；');
                
                for (int i = 0; i < str1.Length; i++)
                {
                    string a=str1[i];
                    Enter.Entity.T106_GoodClassPropertyEntity.EnumchooseType type=Enter.Entity.T106_GoodClassPropertyEntity.EnumchooseType.Input;

                    string[] va = a.Split('：');


                    if (va[1].Contains("|"))
                    {
                        type = Enter.Entity.T106_GoodClassPropertyEntity.EnumchooseType.Multiple;
                        save(code.ToString(), va[0], filename.Substring(0, 2), filename, type);
                        string[] vs = va[1].Trim('|').Split('|');
                        
                        for(int j=0;j<vs.Length;j++)
                        {
                            string v=vs[j];
                            string vcode = code + "_" + j.ToString();
                            save1(code.ToString(), vcode, v);
                        }
                    }
                    else if (va[1].Contains("、"))
                    {
                        type = Enter.Entity.T106_GoodClassPropertyEntity.EnumchooseType.Radio;
                        save(code.ToString(), va[0], filename.Substring(0, 2), filename, type);
                        string[] vs = va[1].Trim('、').Split('、');

                        for (int j = 0; j < vs.Length; j++)
                        {
                            string v = vs[j];
                            string vcode = code + "_" + j.ToString();
                            save1(code.ToString(), vcode, v);
                        }
                    }
                    else if (va[1] == "")
                    {
                        save(code.ToString(), va[0], filename.Substring(0, 2), filename, type);
                    }
                    code++;
                }
            }
        }
        private int save(string code, string name, string ParentID, string ParentID1,Enter.Entity.T106_GoodClassPropertyEntity.EnumchooseType type)
        {
            T106_GoodClassPropertyEntity model = new T106_GoodClassPropertyEntity();
            T106_GoodClassPropertyBLLSub bll = new T106_GoodClassPropertyBLLSub();

            model.PNo = code;

            model.PName = name.Trim();
            model.RootClassID = ParentID;
          model.Weight = 0;
            model.ClassID = ParentID1;
            model.Status = EnumStatus.Valid;
            model.chooseType = type;
            model.PorpertyType = Enter.Entity.T106_GoodClassPropertyEntity.EnumPorpertyType.Base;
          

           return bll.Save(model);
        }
        private void save1(string pcode,string vcode, string name)
        {
            T116_GoodClassProperty_ValueEntity model = new T116_GoodClassProperty_ValueEntity();
            T116_GoodClassProperty_ValueBLLSub bll = new T116_GoodClassProperty_ValueBLLSub();
            model.VNO = vcode;
            model.PNo = pcode;

            model.VName= name.Trim();
            
            model.Weight = 0;
         
            model.Status = EnumStatus.Valid;
       
             bll.Save(model);
        }
    }
}